#!/usr/bin/python
#
# Copyright (C) 2008 rambla.be
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#      http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

""" Contains the objects required for RATS user resource access. """

try:
  from xml.etree import cElementTree as ElementTree
except ImportError:
  try:
    import cElementTree as ElementTree
  except ImportError:
    from elementtree import ElementTree

import atom
import rawsc
from rawsc import RAWS_NAMESPACE
from rawsc.rass import *

class UserParams(rawsc.RawsParams):
    """ Handler for the <raws:params> element (= sub-element of atom:content). """
    _children = {}
    _children['{%s}username' % rawsc.RAWS_NAMESPACE] = ('username', Username)
    _children['{%s}first_name' % RAWS_NAMESPACE] = ('first_name', FirstName)
    _children['{%s}last_name' % RAWS_NAMESPACE] = ('last_name', LastName)
    _children['{%s}email' % RAWS_NAMESPACE] = ('email', Email)
    _children['{%s}password' % RAWS_NAMESPACE] = ('password', Password)

    def __init__(self, username=None, first_name=None, last_name=None, email=None, password = None):
        rawsc.RawsParams.__init__(self, extension_elements=None, extension_attributes=None)
        self.username = Username(text = username)
        self.first_name = FirstName(text = first_name)
        self.last_name = LastName(text = last_name)
        self.email = Email(text = email)
        self.password = Password(text = password)

def UserParamsFromString(xml_string):
    return atom.CreateClassFromXMLString(UserParams, xml_string)   


class UserContent(rawsc.RawsContent):
    """ Handler for the atom:content element, in a RAWS context. """
    _children = atom.AtomBase._children.copy()
    _children['{%s}params' % (RAWS_NAMESPACE)] = ('params', UserParams)
    
    params = None
    
    def __init__(self, params=None):
        rawsc.RawsContent.__init__(self, extension_elements=None, extension_attributes=None)
        self.params = UserParams()
        if params:
            self.params = params
    
def UserContentFromString(xml_string):
    return atom.CreateClassFromXMLString(UserContent, xml_string)   
    

class UserEntry(rawsc.RawsEntry):
    """ Handler for an entry that is returned by the RATS user resource."""
    _children = rawsc.RawsEntry._children.copy()
    _children['{%s}content' % atom.ATOM_NAMESPACE] = ('content', UserContent)

    def __init__(self, author=None, category=None, atom_id=None, link=None, published=None, title=None, updated=None, content = None, extension_elements=None, extension_attributes=None):
        rawsc.RawsEntry.__init__(self, author=author, category=category, atom_id=atom_id, link=link, published=published, title=title, updated=updated,
                                extension_elements=extension_elements, extension_attributes=extension_attributes)
        self.content = UserContent()
        if content:
            self.content = content

def UserEntryFromString(xml_string):
    return atom.CreateClassFromXMLString(UserEntry, xml_string)
    

class UserFeed(rawsc.RawsFeed, rawsc.LinkFinder):
    """ Handler for a feed that is returned by the RATS user resource."""
    _children = rawsc.RawsFeed._children.copy()
    _children['{%s}entry' % atom.ATOM_NAMESPACE] = ('entry', [UserEntry])

def UserFeedFromString(xml_string):
  return atom.CreateClassFromXMLString(UserFeed, xml_string)

